import asyncio
from pyppeteer import launch

width, height = 1366, 768


async def main():
    browser = await launch(headless=False)
    page = await browser.newPage()
    await page.setViewport({'width': width, 'height': height})
    await page.goto('https://dynamic1.scrape.center/')
    await page.goto('https://dynamic2.scrape.center/')
    # 点击
    await page.waitForSelector('.item .name')
    await page.click('.item .name', options={
        'button': 'right',
        'clickCount': 1,  # 1 or 2
        'delay': 3000,  # 毫秒
    })
    # 后退
    await page.goBack()
    # 前进
    await page.goForward()
    # 刷新
    await page.reload()
    # # 保存 PDF
    # await page.pdf()  # 不支持有头模式
    # 截图
    await page.screenshot()
    # 设置页面 HTML
    await page.setContent('<h2>Hello World</h2>')
    # 设置 User-Agent
    await page.setUserAgent('Python')
    # 设置 Headers
    await page.setExtraHTTPHeaders(headers={})
    # 输入
    await page.goto('https://www.taobao.com')
    await page.type('#q', 'iPad')
    # 输入内容和cookie
    print('HTML:', await page.content())
    print('Cookies:', await page.cookies())
    # 关闭
    await page.close()
    await browser.close()


asyncio.get_event_loop().run_until_complete(main())
